package cn.gson.crm.model.service;

import cn.gson.crm.model.dao.SellDao;
import cn.gson.crm.model.mapper.BusinessMapper;
import cn.gson.crm.model.mapper.SellMapper;
import cn.gson.crm.model.pojos.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class SellService {
    @Autowired
    SellMapper sellMapper;
    @Autowired
    SellDao sellDao;
    @Autowired
    BusinessMapper businessMapper;
    //查询所有销售费用
    public List<Sell> select1(String na){
        return sellMapper.select1(na);
    }
    //删除
    public int update1(String sellReimburse,int sellId){
        return sellMapper.update1(sellReimburse,sellId);
    }
    //查询员工
    public List<Employee> selectEmployee(){return sellMapper.selectEmployee();}
    //新增
    public void addSell(Sell sell,HttpSession session){
        User login = (User)session.getAttribute("login");
        int ygId= (int) login.getEmployees().get(0).getYgId();
        sellDao.save(sell);
        //审核表新增
        Check check = new Check();
        check.setChecksqinName((long)ygId);
        check.setCheckType("销售费用");
        check.setChecktypeid(sell.getSellId());
        check.setCheckDate(sell.getSellTime());
        check.setCheckName(sell.getEmployeeByYgId().getYgId());
        check.setCheckCondition("未审核");
    }
    //查询饼状统计图类型数量
    public Map<String ,Object> selltu(){
        List<Sell> sells=sellMapper.selltu();
        Map<String,Object> map=new HashMap<>();
        List<String> lei =new ArrayList<>();//类型
        List<Integer> shul=new ArrayList<>();//数量
        for(int i=0;i<sells.size();i++){
            lei.add(sells.get(i).getSellType());
            shul.add(sellDao.sellType(sells.get(i).getSellType()));
        }
        System.out.println("类型"+sells);
        map.put("lei",lei);
        map.put("shul",shul);
        return map;
    }
    //查询饼状图审批人和数量
    public Map<String ,Object> selltu2(){
        List<Sell> sells=sellMapper.selltu2();
        Map<String,Object> map=new HashMap<>();
        List<String> lei =new ArrayList<>();//负责人
        List<Integer> shul=new ArrayList<>();//数量
        for(int i=0;i<sells.size();i++){
            lei.add(sells.get(i).getSellName());
            shul.add(sellDao.sellName(sells.get(i).getSellName()));
        }
        map.put("lei",lei);
        map.put("shul",shul);
        return map;
    }
    //查询柱状图
    public Map<String,Object> sellzhu(){
       List<Sell> sells=sellMapper.sellzhu();
       Map<String,Object> map=new HashMap<>();
       List<String> shul1=new ArrayList<>();
       List<List<Integer>> shul2=new ArrayList<>();//员工姓名
       List<Integer> shul=new ArrayList<>();//员工所以的销售费用
        for(int i=0;i<sells.size();i++){
            shul1.add(sells.get(i).getEmployeeByYgId().getYname());
            shul2.add(sellDao.selectzhu(shul1.get(i)));
        }
        map.put("shul1",shul1);
        map.put("shul2",shul2);
        System.out.println(map);
        int c=shul2.size();
        int[] arr=new int[shul2.size()];
        for(int i=0;i<shul2.size();i++){
            int d=0;
            int e=shul2.get(i).size();
            for(int j=0;j<e;j++){
                d+=shul2.get(i).get(j);
            }
            arr[i]=d;
            System.out.println(d);
        }
        map.put("shul2",arr);
        return map;
    }
    //查询柱状图2
    public Map<String,Object> sellzhu2(){
        List<Sell> sells=sellMapper.sellzhu2();
        Map<String,Object> map=new HashMap<>();
        List<String> shul1=new ArrayList<>();
        List<List<Integer>> shul2=new ArrayList<>();//员工姓名数量
        List<Integer> shul=new ArrayList<>();//月份分布
        for(int i=0;i<sells.size();i++){
            shul1.add(sells.get(i).getEmployeeByYgId().getYname());
            shul2.add(sellDao.selectzhu2(shul1.get(i)));
        }
        map.put("shul1",shul1);
        map.put("shul2",shul2);
        System.out.println(map);
        int c=shul2.size();
        int[] arr=new int[shul2.size()];
        for(int i=0;i<shul2.size();i++){
            int d=0;
            int e=shul2.get(i).size();
            for(int j=0;j<e;j++){
                d+=shul2.get(i).get(j);
            }
            arr[i]=d;
            System.out.println(d);
        }
        map.put("shul2",arr);
        return map;
    }

}
